﻿using System;
using System.Collections;
using System.Configuration;
using System.ComponentModel;
using System.Data;
using System.Linq;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Text;

using LinkCard.Utils;
using LinkCard.Model;

namespace LinkCard.CustomerView
{
    public partial class RegisterPayment : System.Web.UI.Page
    {
        private Int64 iCustomerID = 0;
        private string sGUID = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["iCustomerID"] != null)
            {
                //Lấy thông tin User
                iCustomerID = Convert.ToInt64(Request.QueryString["iCustomerID"].ToString());
                LoadCustomer();
                //Cài đặt các tham số cần thiết cho OnePay Visa
                var virtualPaymentClientURL = "https://onepay.vn/vpcpay/vpcpay.op";
                var virtualPaymentClientVNUrl = "https://onepay.vn/onecomm-pay/vpc.op";
                
                var againLink = "http://linkcard.vn/RegisterPayment.aspx";
                
                var title = "LinkCard Paygate";
                var version = "2";
                var command = "pay";
                var merchant = "LINKCARD";
                var merchantVN = "LINKCARD";

                var accesscode = "0F488810";
                var accesscodeVN = "PWRDR1JI";

                var merchTxnRef = "LinCard_" + iCustomerID.ToString();
                var orderInfo = "Khach hang: " + iCustomerID.ToString();
                var amount = 2000000;
                
                var returnURL = "http://linkcard.vn/RegisterResult.aspx?option=en";
                var returnURLVN = "http://linkcard.vn/RegisterResult.aspx?option=vn";
           
                var tickerNo = "112.213.94.223";
                //var tickerNo = "127.0.0.1";
                var currency = "VND";

                string SECURE_SECRET = "8C630EE92449FB38AC615381ED92BB81";
                string SECURE_SECRET_VN = "331A7B5348C9F2807ADEC97A91EEBAFB";

                if (Request.QueryString["paymentmethod"] == null)
                {
                    // Khoi tao lop thu vien va gan gia tri cac tham so gui sang cong thanh toan
                    VPCRequest conn = new VPCRequest(virtualPaymentClientURL);
                    conn.SetSecureSecret(SECURE_SECRET);
                    // Add the Digital Order Fields for the functionality you wish to use
                    // Core Transaction Fields
                    conn.AddDigitalOrderField("AgainLink", againLink);
                    conn.AddDigitalOrderField("Title", title);
                    conn.AddDigitalOrderField("vpc_Locale", "vn");//Chon ngon ngu hien thi tren cong thanh toan (vn/en)
                    conn.AddDigitalOrderField("vpc_Version", version);
                    conn.AddDigitalOrderField("vpc_Command", command);
                    conn.AddDigitalOrderField("vpc_Merchant", merchant);
                    conn.AddDigitalOrderField("vpc_AccessCode", accesscode);
                    conn.AddDigitalOrderField("vpc_MerchTxnRef", merchTxnRef);
                    conn.AddDigitalOrderField("vpc_OrderInfo", orderInfo);
                    conn.AddDigitalOrderField("vpc_Amount", amount.ToString());
                    conn.AddDigitalOrderField("vpc_ReturnURL", returnURL);
                    // Thong tin them ve khach hang. De trong neu khong co thong tin
                    conn.AddDigitalOrderField("vpc_SHIP_Street01", "");
                    conn.AddDigitalOrderField("vpc_SHIP_Provice", "");
                    conn.AddDigitalOrderField("vpc_SHIP_City", "");
                    conn.AddDigitalOrderField("vpc_SHIP_Country", "Vietnam");
                    conn.AddDigitalOrderField("vpc_Customer_Phone", "");
                    conn.AddDigitalOrderField("vpc_Customer_Email", "");
                    conn.AddDigitalOrderField("vpc_Customer_Id", "LinkCard_PayGate");
                    // Dia chi IP cua khach hang
                    conn.AddDigitalOrderField("vpc_TicketNo", tickerNo);
                    // Chuyen huong trinh duyet sang cong thanh toan
                    String url = conn.Create3PartyQueryString();
                    Page.Response.Redirect(url);
                }
                else if (Request.QueryString["paymentmethod"].ToString() == "vn")
                {
                    // Khoi tao lop thu vien va gan gia tri cac tham so gui sang cong thanh toan
                    VPCRequest conn = new VPCRequest(virtualPaymentClientVNUrl);
                    conn.SetSecureSecret(SECURE_SECRET_VN);
                    // Add the Digital Order Fields for the functionality you wish to use
                    // Core Transaction Fields
                    conn.AddDigitalOrderField("AgainLink", againLink);
                    conn.AddDigitalOrderField("Title", title);
                    conn.AddDigitalOrderField("vpc_Locale", "vn");//Chon ngon ngu hien thi tren cong thanh toan (vn/en)
                    conn.AddDigitalOrderField("vpc_Version", version);
                    conn.AddDigitalOrderField("vpc_Command", command);
                    conn.AddDigitalOrderField("vpc_Merchant", merchantVN);
                    conn.AddDigitalOrderField("vpc_AccessCode", accesscodeVN);
                    conn.AddDigitalOrderField("vpc_MerchTxnRef", merchTxnRef);
                    conn.AddDigitalOrderField("vpc_OrderInfo", orderInfo);
                    conn.AddDigitalOrderField("vpc_Amount", amount.ToString());
                    conn.AddDigitalOrderField("vpc_ReturnURL", returnURLVN);
                    conn.AddDigitalOrderField("vpc_Currency", currency);
                    // Thong tin them ve khach hang. De trong neu khong co thong tin
                    conn.AddDigitalOrderField("vpc_SHIP_Street01", "");
                    conn.AddDigitalOrderField("vpc_SHIP_Provice", "");
                    conn.AddDigitalOrderField("vpc_SHIP_City", "");
                    conn.AddDigitalOrderField("vpc_SHIP_Country", "Vietnam");
                    conn.AddDigitalOrderField("vpc_Customer_Phone", "");
                    conn.AddDigitalOrderField("vpc_Customer_Email", "");
                    conn.AddDigitalOrderField("vpc_Customer_Id", "LinkCard_PayGate");
                    // Dia chi IP cua khach hang
                    conn.AddDigitalOrderField("vpc_TicketNo", tickerNo);
                    // Chuyen huong trinh duyet sang cong thanh toan
                    String url = conn.Create3PartyQueryString();
                    Page.Response.Redirect(url);
                }
            }
        }

        private void LoadCustomer()
        {
            try
            {
                TopLinkEntities db = new TopLinkEntities();
                var customer = db.Customers.Where(t => t.CustomerID == iCustomerID).FirstOrDefault();

                sGUID = customer.GUID;
            }
            catch
            {
 
            }
        }
    }
}